package com.blrs.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.RepeatSubmit;
import com.blrs.common.constant.HttpStatus;
import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.domain.*;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.domain.vo.ContractPaymentMethodVo;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.service.project.info.IProjectInfoService;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 项目信息 Controller
 * 
 * @author ruoyi
 * @date 2023-11-30
 */
@RestController
@RequestMapping("/project/projectInfo")
public class ProjectInfoController extends BaseController
{
    @Autowired
    private IProjectInfoService projectInfoService;

    //主项目变更
    @Log(title = "主项目变更", businessType = BusinessType.CHANGE)
    @PostMapping("/editProjectInfoChange")
    public AjaxResult editProjectInfoChange(@RequestBody ProjectInfo projectInfo, String changNote)throws NoSuchFieldException, IllegalAccessException
    {
        return toAjax(projectInfoService.updateProjectInfoChange(projectInfo,changNote));
    }
    //子项目变更
    @Log(title = "子项目变更", businessType = BusinessType.CHANGE)
    @PostMapping("/editSubProjectInfoChange")
    public AjaxResult editSubProjectInfoChange(@RequestBody SubProjectInfo subProjectInfo, String changNote)throws NoSuchFieldException, IllegalAccessException
    {
        return toAjax(projectInfoService.updateSubProjectInfoChange(subProjectInfo,changNote));
    }

    //主子列表
    @GetMapping("/listProjectTree")
    public TableDataInfo listProjectTree(ProjectInfo projectInfo, SubProjectInfo subProjectInfo,@RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
        projectInfo.setBelongTo(SecurityUtils.getOrgId().toString());
        List<Map> list = projectInfoService.getProjectTree(projectInfo,subProjectInfo);
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, list.size());
        List<Map> listPage = list.subList(start, end);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(listPage);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    //修改主分类
    @PostMapping("/editMainClass")
    public AjaxResult editMainClass(@RequestBody BusinessMainClassVo businessMainClassVo) {
        return toAjax(projectInfoService.editMainClass(businessMainClassVo));
    }

    //提交 修改项目状态
    @PostMapping("/editState")
    public AjaxResult editState(@RequestBody ProjectInfo projectInfo) {
        return toAjax(projectInfoService.editState(projectInfo));
    }

    //项目变更时删除子项目时 修改子项目以及相关联的采购合作状态为终止
    @PostMapping("/editSubState")
    public AjaxResult editSubState(@RequestBody String subId,String subType) {
        return toAjax(projectInfoService.editSubState(subId,subType));
    }
    /**
     * 查询项目信息列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
    }

    @GetMapping("/otherList")
    public TableDataInfo otherList(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoOtherList(projectInfo);
        return getDataTable(list);
    }

    @GetMapping("/listProjectAll")
    public TableDataInfo listProjectAll(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoListAll(projectInfo);
        return getDataTable(list);
    }
    //查询状态为评价/已完结的项目
    @GetMapping("/listOver")
    public TableDataInfo listOver(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoListOver(projectInfo);
        return getDataTable(list);
    }
    @GetMapping("/listSub")
    public TableDataInfo listSub(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectSubProjectInfoAllList(projectInfo);
        return getDataTable(list);

    }

    //主子列表
    @GetMapping("/listSubProjectTree")
    public TableDataInfo listSubProjectTree(ProjectInfo projectInfo, SubProjectInfo subProjectInfo, @RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
//        startPage();
        List<Map> list = projectInfoService.getSubProjectTree(projectInfo,subProjectInfo);
//        return success(list);
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, list.size());
        List<Map> listPage = list.subList(start, end);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(listPage);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 导出项目信息列表
     */

    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectInfo projectInfo)
    {
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        ExcelUtil<ProjectInfo> util = new ExcelUtil<ProjectInfo>(ProjectInfo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */

    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return success(projectInfoService.selectProjectInfoByProjectId(projectId));
    }

    /**
     * 新增项目信息
     */

    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult add(@RequestBody ProjectInfo projectInfo)
    {
        return toAjax(projectInfoService.insertProjectInfo(projectInfo));
    }

    /**
     * 修改项目信息
     */
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult edit(@RequestBody ProjectInfo projectInfo)
    {
        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }

    /**
     * 删除项目信息
     */
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return toAjax(projectInfoService.deleteProjectInfoByProjectIds(projectIds));
    }

    /**
     * 根据项目id查询项目以及客户名称和客户id
     */
    @GetMapping("/selectProjectContract/{projectId}")
    public AjaxResult selectProjectByContract(@PathVariable(name = "projectId")String projectId){
        return AjaxResult.success(projectInfoService.selectProjectInfoByProjectIdAndContractId(projectId));
    }

    /**
     * 只查询交付的当前公司项目
     * @param projectInfo
     * @return
     */
    @GetMapping("/workList")
    public TableDataInfo workList(ProjectInfo projectInfo)
    {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoWorkListList(projectInfo);
        return getDataTable(list);
    }



}
